<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item label="出库信息ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="informationId">
          <j-tree-select
            ref="treeSelect"
            placeholder="请选择出库信息ID"
            v-model="model.informationId"
            dict="buss_outbound_order_detail,material_name,id"
            pidField="information_id"
            pidValue="0"
            hasChildField="has_child"
            >
          </j-tree-select>
        </a-form-model-item>
        <a-form-model-item label="物料ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialId">
          <a-input v-model="model.materialId" placeholder="请输入物料ID" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialCode">
          <a-input v-model="model.materialCode" placeholder="请输入物料编号" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialName">
          <a-input v-model="model.materialName" placeholder="请输入物料名称" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noneSpec">
          <a-input v-model="model.noneSpec" placeholder="请输入规格型号" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="主计量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="meUnit">
          <j-dict-select-tag type="list" v-model="model.meUnit"  dictCode="ji_unit" placeholder="请选择主计量" />
        </a-form-model-item>
        <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quantity">
          <a-input-number v-model="model.quantity" placeholder="请输入数量" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="批次号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="batchNumber">
          <a-input v-model="model.batchNumber" placeholder="请输入批次号" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="物料批号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialBatchNumber">
          <a-input v-model="model.materialBatchNumber" placeholder="请输入物料批号" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="库位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehouseLocation">
          <a-input v-model="model.warehouseLocation" placeholder="请输入库位" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="入库数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inventoryQuantity">
          <a-input-number v-model="model.inventoryQuantity" placeholder="请输入入库数量" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="已出数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="outputQuantity">
          <a-input-number v-model="model.outputQuantity" placeholder="请输入已出数量" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="宝洁供应商编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bjSupplierCode">
          <a-input v-model="model.bjSupplierCode" placeholder="请输入宝洁供应商编码" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="宝洁供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bjSupplierName">
          <a-input v-model="model.bjSupplierName" placeholder="请输入宝洁供应商" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="入库ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehousingId">
          <a-input v-model="model.warehousingId" placeholder="请输入入库ID" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="入库单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehousingCode">
          <a-input v-model="model.warehousingCode" placeholder="请输入入库单号" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="入库日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehousingDate">
          <j-date placeholder="请选择入库日期" v-model="model.warehousingDate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="限用日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="restrictedDate">
          <j-date placeholder="请选择限用日期" v-model="model.restrictedDate" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="宝洁单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bjNo">
          <a-input v-model="model.bjNo" placeholder="请输入宝洁单号" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="占用数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="occupationQuantity">
          <a-input-number v-model="model.occupationQuantity" placeholder="请输入占用数量" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="辅助单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auUnit">
          <j-dict-select-tag type="list" v-model="model.auUnit"  dictCode="zl_unit" placeholder="请选择辅助单位" />
        </a-form-model-item>
        <a-form-model-item label="辅助数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auQuantity">
          <a-input-number v-model="model.auQuantity" placeholder="请输入辅助数量" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="入库辅助数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inAuQuantity">
          <a-input-number v-model="model.inAuQuantity" placeholder="请输入入库辅助数量" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="已出辅助数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="outAuQuantity">
          <a-input-number v-model="model.outAuQuantity" placeholder="请输入已出辅助数量" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="占用辅助数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="occupationAuQuantity">
          <a-input-number v-model="model.occupationAuQuantity" placeholder="请输入占用辅助数量" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="单位换算率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="conversionRate">
          <a-input v-model="model.conversionRate" placeholder="请输入单位换算率" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierId">
          <j-dict-select-tag type="list" v-model="model.supplierId"  dictCode="erp_supplier,name,id" placeholder="请选择供应商" />
        </a-form-model-item>
        <a-form-model-item label="生产车间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productionWorkshop">
          <j-dict-select-tag type="list" v-model="model.productionWorkshop"  dictCode="sys_depart,depart_name,id" placeholder="请选择生产车间" />
        </a-form-model-item>
        <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerName">
          <a-input v-model="model.customerName" placeholder="请输入客户名称" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
          <a-input v-model="model.remark" placeholder="请输入备注" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="订单编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNo">
          <a-input v-model="model.orderNo" placeholder="请输入订单编号" ></a-input>
        </a-form-model-item>
        <a-form-model-item label="父级节点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pid">
          <a-input v-model="model.pid" placeholder="请输入父级节点" ></a-input>
        </a-form-model-item>
        
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  export default {
    name: "BussOutboundOrderDetailModal",
    components: { 
    },
    data () {
      return {
        title:"操作",
        width:800,
        visible: false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/outbound/bussOutboundOrderDetail/add",
          edit: "/outbound/bussOutboundOrderDetail/edit",
        },
        expandedRowKeys:[],
        pidField:"informationId"
     
      }
    },
    created () {
       //备份model原始值
       this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (obj) {
        this.modelDefault.informationId=''
        this.edit(Object.assign(this.modelDefault , obj));
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate()
      },
      handleOk () {
        const that = this;
        // 触发表单验证
       this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
             if(this.model.id && this.model.id === this.model[this.pidField]){
              that.$message.warning("父级节点不能选择自己");
              that.confirmLoading = false;
              return;
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      handleCancel () {
        this.close()
      },
      submitSuccess(formData,flag){
        if(!formData.id){
          let treeData = this.$refs.treeSelect.getCurrTreeData()
          this.expandedRowKeys=[]
          this.getExpandKeysByPid(formData[this.pidField],treeData,treeData)
          this.$emit('ok',formData,this.expandedRowKeys.reverse());
        }else{
          this.$emit('ok',formData,flag);
        }
      },
      getExpandKeysByPid(pid,arr,all){
        if(pid && arr && arr.length>0){
          for(let i=0;i<arr.length;i++){
            if(arr[i].key==pid){
              this.expandedRowKeys.push(arr[i].key)
              this.getExpandKeysByPid(arr[i]['parentId'],all,all)
            }else{
              this.getExpandKeysByPid(pid,arr[i].children,all)
            }
          }
        }
      }
      
      
    }
  }
</script>